package com.shareyi.study.springboot.common;

import com.shareyi.joywindow.JoyWindowData;
import com.shareyi.joywindow.JoyWindowIntf;
import com.shareyi.joywindow.window.JoyWindowSwing;
import com.shareyi.study.springboot.common.utils.LogHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @autor: zhangshibin
 * @description 描述
 * @since 2018/6/20
 */
public class JoyWindowHolder {

    final static Logger LOGGER = LoggerFactory.getLogger(JoyWindowHolder.class);

    public static final JoyWindowHolder instance = new JoyWindowHolder();


    public static JoyWindowHolder getInstance(){
        return instance;
    }


    public void init(String port){
        System.setProperty("java.awt.headless","false");
        JoyWindowIntf intf=null;

            intf= JoyWindowSwing.getInstance();
            try {
                intf.init();		//初始化
                LogHelper.DEFAULT.info("读取配置文件，同时启动server 和 swing");
            }catch (Exception e) {
                LOGGER.error("启动Swing窗口失败",e);
                System.exit(0);
            }


        JoyWindowData.setJoyWindow(intf);
        if(intf!=null){
            try {
                intf.openWindow();	//显示界面
                StringBuilder stringBuilder=new StringBuilder();
                String httpUrl="http://127.0.0.1:"+port+"/";
                stringBuilder.append("<html><h1>说明</h1> 本窗口仅仅提供文件选择器功能，体验较差，请使用windows版本的！");
                stringBuilder.append("<p>服务已经启动，请用浏览器打开地址访问：</p>");
                stringBuilder.append("<a href=\"");
                stringBuilder.append(httpUrl).append("\">");
                stringBuilder.append(httpUrl).append("</a>");
                stringBuilder.append("</html>");
                intf.setText(stringBuilder.toString());

                LOGGER.info("服务器已经启动，窗口已经打开！");
            } catch (Exception e) {
                LOGGER.error("界面初始化失败");
                System.exit(0);
            }
        }

    }


}
